'use strict';
// const fs = require('fs');
const Controller = require('egg').Controller;

class PageController extends Controller {
  async get() {
    const id = this.ctx.request.body.id || this.ctx.query.id;
    const page = await this.ctx.service.page.find(id)
    this.ctx.body = this.ctx.success(page);
  }
  async page() {
    const user = await this.app.mysql.get('page', { id: 1 });

    this.ctx.body = { user };
  }
  async list() {
    const queryObj = this.ctx.request.body.name ? this.ctx.request.body : this.ctx.query;
    queryObj.page = queryObj.page || 1;
    queryObj.pageSize = queryObj.pageSize || 20;
    let pageData = await this.app.mysql.select('page', {
      orders: [['id','desc']], // 排序方式
      limit: queryObj.pageSize, // 返回数据量
      offset: queryObj.page * queryObj.pageSize - queryObj.pageSize, // 数据偏移量
    });
    this.ctx.body = this.ctx.success(pageData);
  }
  async add() {
    const queryObj = this.ctx.request.body.name ? this.ctx.request.body : this.ctx.query;
    queryObj.seed = "{}";
    const result = await this.app.mysql.insert('page', queryObj);
    this.ctx.body = this.ctx.success(result);
  }

  async update() {
    const queryObj = this.ctx.request.body.id ? this.ctx.request.body : this.ctx.query;
    const result = await this.app.mysql.update('page', queryObj);
    this.ctx.body = this.ctx.success(result);
  }
}

module.exports = PageController;
